package com.kingdee.eas.qx.util;

  import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
/*    */ public class MD5Util
/*    */ {
/*    */   private static String byte2Hex(byte b)
/*    */   {
/*  8 */     int value = (b & 0x7F) + ((b < 0) ? 128 : 0);
/*  9 */     return new StringBuilder().append((value < 16) ? "0" : "").append(Integer.toHexString(value).toLowerCase()).toString();
/*    */   }
/*    */
/*    */   public static String MD5_32(String var, String charset) throws NoSuchAlgorithmException, UnsupportedEncodingException
/*    */   {
/* 14 */     MessageDigest md5 = MessageDigest.getInstance("MD5");
/* 15 */     StringBuffer strbuf = new StringBuffer();
/*    */ 
/* 17 */     md5.update(var.getBytes(charset), 0, var.length());
/* 18 */     byte[] digest = md5.digest();
/*    */ 
/* 20 */     for (int i = 0; i < digest.length; ++i) {
/* 21 */       strbuf.append(byte2Hex(digest[i]));
/*    */     }
/*    */ 
/* 24 */     return strbuf.toString();
/*    */   }
/*    */
/*    */   public static String MD5_16(String var,String charset) throws NoSuchAlgorithmException, UnsupportedEncodingException {
/* 28 */     return MD5_32(var,charset).subSequence(8, 24).toString();
/*    */   }


		public static String md5(String str) {
			  StringBuffer sb = new StringBuffer(32);
			  try {
			   MessageDigest md = MessageDigest.getInstance("MD5");
		       byte[] array = md.digest(str.getBytes("UTF-8"));
		       for (int i = 0; i<array.length;i++)
		        sb.append(Integer.toHexString((array[i] & 0xFF) | 0x100).toUpperCase().substring(1, 3));   
		     } catch (Exception e) {
		      e.printStackTrace();
		      return null;   
		   }
		  return sb.toString();
		 }

 }

